package com.example.myui.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.myui.R;

import java.util.ArrayList;

import cn.addapp.pickers.listeners.OnItemPickListener;
import cn.addapp.pickers.listeners.OnSingleWheelListener;
import cn.addapp.pickers.picker.DatePicker;
import cn.addapp.pickers.picker.SinglePicker;

public class PickerActivity extends AppCompatActivity {

    private TextView textView;
    private TextView textView1;
    private TextView time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picker);
        initView();
    }

    private void initView() {
        time = findViewById(R.id.tv_result_time);
        textView = findViewById(R.id.tv_result_year);
        textView1 = findViewById(R.id.tv_result_month);

        findViewById(R.id.btn_picktime).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<String> list = new ArrayList<>();
                for (int i = 0; i < 60; i++) {
                    String s = "";
                    if (i < 10) {
                        s = "0" + i;
                    } else {
                        s = i + "";
                    }
                    list.add(s);
                }
//        String[] ss = (String[]) list.toArray();
                SinglePicker<String> picker = new SinglePicker<>(PickerActivity.this, list);
                picker.setCanLoop(false);//不禁用循环
                picker.setLineVisible(true);
                picker.setShadowVisible(true);
                picker.setTextSize(25);
                picker.setSelectedIndex(1);
                picker.setWheelModeEnable(true);
                //启用权重 setWeightWidth 才起作用
//                    picker.setLabel("分");
                picker.setWeightEnable(true);
                picker.setWeightWidth(1);
                picker.setSelectedTextColor(0xFF279BAA);//前四位值是透明度
                picker.setUnSelectedTextColor(0xFF999999);
                picker.setOnSingleWheelListener(new OnSingleWheelListener() {
                    @Override
                    public void onWheeled(int index, String item) {
//                            showToast("index=" + index + ", item=" + item);

                    }
                });
                picker.setOnItemPickListener(new OnItemPickListener<String>() {
                    @Override
                    public void onItemPicked(int index, String item) {
//                            showToast("index=" + index + ", item=" + item);
                        time.setText(item);
                    }
                });
                picker.show();
            }
        });

        findViewById(R.id.btn_pick_date).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final DatePicker picker = new DatePicker(PickerActivity.this, DatePicker.YEAR_MONTH);
                picker.setTitleText("出生日期");
                picker.setTitleTextColor(0xFF999999);
                picker.setTitleTextSize(12);
                picker.setCanLoop(false);
                picker.setWheelModeEnable(true);
                picker.setTopPadding(15);
                picker.setRangeStart(2016, 8, 29);
                picker.setRangeEnd(2111, 1, 11);
                picker.setSelectedItem(2017, 9);
                picker.setWeightEnable(true);
                picker.setWheelModeEnable(true);
                picker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
                    @Override
                    public void onDatePicked(String year, String month) {
                        textView.setText(year);
                        textView1.setText(month);
                    }
                });
                picker.setOnWheelListener(new DatePicker.OnWheelListener() {
                    @Override
                    public void onYearWheeled(int i, String s) {

                    }

                    @Override
                    public void onMonthWheeled(int i, String s) {

                    }

                    @Override
                    public void onDayWheeled(int i, String s) {

                    }
                });
                picker.show();
            }
        });
    }
}
